#!/usr/bin/env python

############################################################################
#  QLyrics - A simple lyrics viewer.                                       #
#  Copyright (C) 2008  hsys <hanshuiys@gmail.com>                          #
#                                                                          #
#  This program is free software; you can redistribute it and/or modify    #
#  it under the terms of the GNU General Public License as published by    #
#  the Free Software Foundation; either version 3 of the License, or       #
#  (at your option) any later version.                                     #
#                                                                          #
#  This program is distributed in the hope that it will be useful,         #
#  but WITHOUT ANY WARRANTY; without even the implied warranty of          #
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           #
#  GNU General Public License for more details.                            #
#                                                                          #
#  You should have received a copy of the GNU General Public License       #
#  along with this program; if not, write to the Free Software Foundation, #
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA       #
############################################################################

import os, sys, signal, re, libqlyrics;

def getTrackTitle():
    return os.popen('dcop amarok player title').read().strip()

def getTrackAlbum():
    return os.popen('dcop amarok player album').read().strip()

def getTrackArtist():
    return os.popen('dcop amarok player artist').read().strip()

def getTrackLength():
    return repr(int(os.popen('dcop amarok player trackTotalTime').read()) * 1000)

def getTrackFilename():
    return os.popen('dcop amarok player path').read().strip()

def getTrackTime():
    return os.popen('dcop amarok player trackCurrentTimeMs').read().strip()

def sendPlayerStatus():
    StatusTable = {
            "0" : "Idle",
            "1" : "Paused",
            "2" : "Playing"
            }
    status = os.popen('dcop amarok player status').read().strip()
    if (not (status in StatusTable)):
        return status
    status = StatusTable[status]
    libqlyrics.statusChanged(status)
    return status

def cleanup(signum, frame):
    libqlyrics.stop()

signal.signal(signal.SIGTERM, cleanup)

libqlyrics.setEventHandler("Title", getTrackTitle)
libqlyrics.setEventHandler("Album", getTrackAlbum)
libqlyrics.setEventHandler("Artist", getTrackArtist)
libqlyrics.setEventHandler("Length", getTrackLength)
libqlyrics.setEventHandler("Filename", getTrackFilename)
libqlyrics.setEventHandler("Time", getTrackTime)

libqlyrics.start()

if (sendPlayerStatus() != "Idle"):
    libqlyrics.trackChanged()

while True:
    notify = sys.stdin.readline();

    if (not libqlyrics.isRunning()):
        sys.exit()

    if (re.search('configure', notify)):
        libqlyrics.showConfigureWindow()
    elif (re.search('engineStateChange', notify)):
        sendPlayerStatus()
    elif (re.search('trackChange', notify)):
        libqlyrics.trackChanged()

